$(function(){
	jeDate("#fdate",{
		format:"YYYY-MM-DD",
		isTime:false,
	});
	var nowMonth = jeDate.timeStampDate(Math.round(new Date()/1000),'YYYY-MM-DD');
	$('#fdate').val(nowMonth);
	
	$('#procedureP').selectpicker('destroy');
	
	procedureSelectP();
	resizeIframeHight();
    formatTitle();
    searchTable();
    
	
});

function procedureSelectP() {
	$.ajax({
		url : pathUrl("bill/findCustom.action"),
		type : "POST",
		dataType : "JSON",
		async : false,
		success : function(ret) {
			var val = ret;
			var option = "";
			if(val.length != 0){
				$("#procedureP").empty();
				for (x in val) {
					var str = val[x].fn;
					option += '<option value="' + val[x].id + '">' + str + '</option>';
				}
			}
			$("#procedureP").html(option);
			$('#procedureP').selectpicker('refresh');
		}
	});
}

var title='';
function formatTitle(){
    var date = new Date($('#fdate').val());
    week = date.getDay();
    day = date.getDate();
    if( week == 0){
        　　week = 7;
        }
        var config={
            　　getMonth: date.getMonth()+1,
            　　getYear: date.getFullYear(),
            　　getWeek: Math.ceil((day + 6 - week) / 7),
            }
   
     $("#title").html(
        $("#procedureP option:selected").text()+'项目'+config.getMonth+'月第'+config.getWeek+'周工时统计'
     );
     title=$("#procedureP option:selected").text()+'项目'+config.getMonth+'月第'+config.getWeek+'周工时统计';
}
$('#query').click(function(){
    formatTitle();
    searchTable();
})
function searchTable(){
    const columns=[
        [
            {
                title:$("#procedureP option:selected").text(),
                colspan:2,
                align:'center',
                valign:'middle',
            }
        ],[
            {
                title:'序号',
                field:'index',
                align:'center',
                valign:'middle',
                width:100,
                footerFormatter:function(){
                    return '总计'
                }

            },
            {
                title:'姓名',
                field:'name',
                align:'center',
                valign:'middle',
                width:150,
            }
        ]
    ];
    const weeks=[
        '周一',
        '周二',
        '周三',
        '周四',
        '周五',
        '周六',
        '周日'
    ]
    for(let i=0;i<7;i++){
        columns[0].push({
            title:moment($('#fdate').val()).day(i+1).format('YYYY-MM-DD'),
            field:'day'+i,
            align:'center',
            valign:'middle',
            width:100,
        })
        columns[1].push({
            title:weeks[i],
            field:'day'+i,
            align:'center',
            valign:'middle',
            width:100,
        })
    }
    columns[0].push({
        title:'实际投入工时（小时）',
        field:'hsum',
        align:'center',
        valign:'middle',
        rowspan:2,
        width:100,
        formatter:function(value,row,index,field){
            return Number(value).toFixed(1)
        },
        footerFormatter:function(data,value){
            var total=0;
            data.forEach(function(e){
                total+=Number(e.hsum)
            })
            return total.toFixed(1);
        }
    },{
        title:'计划投入工时（小时）',
        field:'hplan',
        align:'center',
        valign:'middle',
        rowspan:2,
        width:100,
        formatter:function(value,row,index,field){
            return Number(value).toFixed(1)
        },
        footerFormatter:function(data,value){
            var total=0;
            data.forEach(function(e){
                total+=Number(e.hplan)
            })
            return total.toFixed(1);
        }
    },{
        title:'偏差公式</br>（实际-计划）（小时）',
        field:'hdiff',
        align:'center',
        valign:'middle',
        rowspan:2,
        width:100,
        cellStyle:function(value,row,index){
            const rate=Math.abs(value/row.hplan);
            var color='';
            if(rate<0.1||value==0){
                color='#8bc34a7d';
            }else if(rate>0.2){
                color='#ff000047';
            }else{
                color='#ffeb3b6b';
            }
            return {
                css:{
                    'background':color
                }
            }
        },
        formatter:function(value,row,index,field){
            return Number(value).toFixed(1)
        },
        footerFormatter:function(data,value){
            var total=0;
            data.forEach(function(e){
                total+=Number(e.hdiff)
            })
            return total.toFixed(1);
        },
    },{
        title:'实际投入工时（天）',
        field:'dsum',
        align:'center',
        valign:'middle',
        rowspan:2,
        width:100,
        formatter:function(value,row,index,field){
            return Number(value).toFixed(1)
        },
        footerFormatter:function(data,value){
            var total=0;
            data.forEach(function(e){
                total+=Number(e.dsum)
            })
            return total.toFixed(1);
        }
    },{
        title:'计划投入工时（天）',
        field:'dplan',
        align:'center',
        valign:'middle',
        rowspan:2,
        width:100,
        formatter:function(value,row,index,field){
            return Number(value).toFixed(1)
        },
        footerFormatter:function(data,value){
            var total=0;
            data.forEach(function(e){
                total+=Number(e.dplan)
            })
            return total.toFixed(1);
        }
    },{
        title:'偏差公式</br>（实际-计划）（天）',
        field:'ddiff',
        align:'center',
        valign:'middle',
        rowspan:2,
        width:100,
        formatter:function(value,row,index,field){
            return Number(value).toFixed(1)
        },
        footerFormatter:function(data,value){
            var total=0;
            data.forEach(function(e){
                total+=Number(e.ddiff)
            })
            return total.toFixed(1);
        },
        cellStyle:function(value,row,index){
            const rate=Math.abs(value/row.dplan);
            var color='';
            if(rate<0.1||value==0){
                color='#8bc34a7d';
            }else if(rate>0.2){
                color='#ff000047';
            }else{
                color='#ffeb3b6b';
            }
            return {
                css:{
                    'background':color
                }
            }
        }
    })

    $.ajax({
        url:pathUrl("bill/findEPZ.action"),
        data:{
            day:$('#fdate').val(),
            pro:$("#procedureP option:selected").text()
        },
        success:function(data){
            data=data.map((el,index)=>({
                ...el,
                index
            }))
            $("#table").bootstrapTable('destroy').bootstrapTable({
                data:data,
                pagination: true,
                            // 初始化加载第几页
                            pageNumber: 1,
                            // 每页的记录行数
                            pageSize: 20,
                            // 分页方式：client客户端分页，server服务端分页（*）
                            sidePagination: "client",
                            showFooter:true,
                            showExport:true,
                            exportDataType: "all",
                            exportTypes: [ 'excel', 'pdf'],
                            exportOptions:{
                                fileName:title
                            },
                            //toolbar:'#toolbar',
        
                            // 每页记录行数选项
                            pageList: [20, 30, 50, 100],
                            // 传递参数
                            queryParams: function (params) {
                              var data = {};
                             data.date=$('#fdate').val();
                             //data.pName=$("#procedureP option:selected").text();
                              return data;
                            },
                            // 数据类型
                            dataType: 'JSON',
                            // 每一行的唯一标识，一般为主键列
                            uniqueId: "id",
                            columns
            })
        }
    })
}

